package cn.gson.crm.control.office;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;

import cn.gson.crm.control.base.BaseServlet;
import cn.gson.crm.model.dao.OfficeDao;
@WebServlet("/office/add")
public class OfficeAddServlet extends BaseServlet{
	private OfficeDao officeDao = new OfficeDao();
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Override
	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String name = request.getParameter("name");
		int id = officeDao.getStuId(name);
		String str  = officeDao.select(id);
		if(str != null && !str.isEmpty()){
			id = -1;
		}
		request.setAttribute("id", id);
		response.setContentType("application/json;charset=utf-8");
    	String jsonStr = JSONArray.toJSONString(id);
        //输出结果
        response.getWriter().append(jsonStr);
	}
	
	@Override
	protected void post(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String stuId =  request.getParameter("stuid");
		String officeCompany =  request.getParameter("officeCompany");
		String officeTryMoney =  request.getParameter("officeTryMoney");
		String officeFormalMoney =  request.getParameter("officeFormalMoney");
		String officeTime =  request.getParameter("officeTime");
		System.out.println(stuId);
		boolean type = officeDao.insertOffice(officeCompany, officeTryMoney, stuId, officeFormalMoney, officeTime);
		if(type){
			officeDao.updateStudent(stuId);
		}
		forward("../../office/manager");
	}

}
